//
//  ShuaxinController.m
//  上拉刷新
//
//  Created by KM on 16/10/31.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "ShuaxinController.h"
#import "WaitCell.h"
#import "ListData.h"
@interface ShuaxinController ()
{
    NSMutableArray<ListItemData *>*da;
    BOOL finished;
    int page;
}
@end

@implementation ShuaxinController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    da = [NSMutableArray new];
    finished = NO;
    page = 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return da.count+1 ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < da.count) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCell" forIndexPath:indexPath];
        ListItemData *li = da[indexPath.row];
        cell.textLabel.text = li.name;
        cell.detailTextLabel.text = li.tel;
        return cell;
    }
    else
    {
        WaitCell *cell = (WaitCell *)[tableView dequeueReusableCellWithIdentifier:@"waitCell" forIndexPath:indexPath];
        [cell.av startAnimating];
        cell.label.text = @"";
        if (finished) {
            [cell.av stopAnimating];
            cell.label.text = @"没有更多数据";
            return cell;
        }
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/list.php?page=%d",page++]];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        NSURLSession *se = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [se dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSString *text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
            NSLog(@"%@",text);
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            ListData *d = [[ListData alloc]initWithDictionary:dict];
            [da addObjectsFromArray:d.record];
            dispatch_async(dispatch_get_main_queue(), ^{
                {
                    if (d.record.count < 10) {
                        finished = YES;
                    }
                }
                [tableView reloadData];
            });
        }];
        [task resume];

        return cell;
    }
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
